package com.study.mall.product.service.impl;

import com.study.mall.product.dao.CategoryBrandRelationDao;
import com.study.mall.product.service.CategoryBrandRelationService;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.study.common.utils.PageUtils;
import com.study.common.utils.Query;

import com.study.mall.product.dao.BrandDao;
import com.study.mall.product.entity.BrandEntity;
import com.study.mall.product.service.BrandService;
import org.springframework.transaction.annotation.Transactional;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {


    @Autowired
    CategoryBrandRelationService categoryBrandRelationService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //增加模糊查询
        //获取搜索框中key的值
        String key = (String) params.get("key");
        //构造查询条件
        QueryWrapper<BrandEntity> wapper = new QueryWrapper<>();
        //如果key不为空则添加条件
        if (!StringUtils.isEmpty(key)){
            wapper.eq("brand_id",key)
                    .or().like("name",key)
                    .or().like("descript",key)
                    .or().like("first_letter",key);
        }
        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params),
                wapper
        );
        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void updateDetail(BrandEntity brand) {

        this.updateById(brand);

        //修改关联表中数据

        if (!Strings.isEmpty(brand.getName())) {
            categoryBrandRelationService.updateBrand(brand.getBrandId(), brand.getName());
        }
    }

}